/**
 * Project Name:emm-marketing
 * File Name:BusinessChangeServiceFacadeImpl.java
 * Package Name:com.emm.yixun.marketing.service.impl
 * Date:2017年11月13日上午11:10:10
 * Copyright (c) 2017, shihao.liu@qiaofang.info All Rights Reserved.
 *
*/

package com.emm.yixun.marketing.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jpush.api.utils.StringUtils;

import com.emm.yixun.basic.service.BusinessChangeService;
import com.emm.yixun.basic.service.DataPermissionService;
import com.emm.yixun.basic.service.HaveReceiveRefundService;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.common.constant.SysConstants;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.exception.XFUNExceptionCode;
import com.emm.yixun.common.model.BusinessChange;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.dto.BusinessChangeDto;
import com.emm.yixun.common.model.dto.HaveReceiveRefundDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.DocTypeEnum;
import com.emm.yixun.common.model.enums.ReceivablesTypeEnum;
import com.emm.yixun.common.model.enums.SignedBusinessStatusEnum;
import com.emm.yixun.common.model.enums.SubscribeBusinessStatusEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.marketing.service.BusinessChangeServiceFacade;

/**
 * ClassName:BusinessChangeServiceFacadeImpl <br/>
 * Date:     2017年11月13日 上午11:10:10 <br/>
 * @author   Administrator
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Service("businessChangeServiceFacade")
@WebService(name = "com.emm.yixun.marketing.service.BusinessChangeServiceFacade")
public class BusinessChangeServiceFacadeImpl implements BusinessChangeServiceFacade {
	
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private DataPermissionService dataPermissionService;
	
	@Autowired
	private BusinessChangeService businessChangeService;
	
	@Autowired
	private HaveReceiveRefundService haveReceiveRefundService;
	
	@Autowired
	private SignedService signedService;
	
	@Override
	public Page<BusinessChange> queryBusinessChangeInfo(BusinessChangeDto dto, Integer begin, Integer row) {
		Page<BusinessChange> response = new Page<BusinessChange>();
		try {
			if(null==dto || null==begin || null==row){
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			if (SysConstants.openDataPermission) {
				String dataPermission = dataPermissionService.dataPermissionCondition(dto.getPermissionUserId(),
						dto.getResourcesCode(), "s", dto.getColumnName());
				String[] dataPermissionArr = dataPermission.split(":");
				if ("00".equals(dataPermissionArr[0])) {
					response.setErrorMsg(XFUNExceptionCode.USER_PRODUCTNOEXIST.getDesin());
					response.setSuccess(false);
					return response;
				}
				dto.setDataPermission(dataPermissionArr[1]);
			}
			dto = setQueryBusinessChangeDto(dto);
			Page<BusinessChange> page = businessChangeService.getModelByPage(dto, begin, row);
			if(null == page.getRows()){
				response.setSuccess(false);
				response.setErrorMsg("分页数据不存在");
				return response;
			}
			page.setRows(fomatBusinessChange(page.getRows()));
			response=page;
			response.setPageSize(row);
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}",e);
		}
		return response;
	}
	
	public BusinessChangeDto setQueryBusinessChangeDto(BusinessChangeDto dto){
		if(!StringUtils.isEmpty(dto.getCheckDateBefore())){
			dto.setCheckDateBefore(dto.getCheckDateBefore()+" 00:00:00");
		}
		if(!StringUtils.isEmpty(dto.getCheckDateAfter())){
			dto.setCheckDateAfter(dto.getCheckDateAfter()+" 23:59:59");
		}
		
		return dto;
	}
	
	public List<BusinessChange> fomatBusinessChange(List<BusinessChange> list){
		for (BusinessChange business:list) {
			business.setAuditStatus(AuditStatusEnum.getByNo(business.getAuditStatus()).getDes());
			String receivablesType = "";
			if(business.getFix().equals("cus")){ //认购业务
				 if(StringUtils.isEmpty(business.getBusinessSecStatus())){
					 business.setBusinessStatus(SubscribeBusinessStatusEnum.getByNo(business.getBusinessStatus()).getDes());
				 }else{
					 business.setBusinessStatus(SubscribeBusinessStatusEnum.getByNo(business.getBusinessSecStatus()).getDes());
				 }
				 BigDecimal haveAmount = haveReceiveRefundService.findSubscribeHaveAmount(business.getId());
				 business.setReceived(haveAmount==null?BigDecimal.ZERO:haveAmount); //认购已收
				 receivablesType = ReceivablesTypeEnum.SUBSCRIBE.getNo();
			}else if(business.getFix().equals("sig")){
				 if(StringUtils.isEmpty(business.getBusinessSecStatus())){
					 business.setBusinessStatus(SignedBusinessStatusEnum.getByNo(business.getBusinessStatus()).getDes());
				 }else{
					 business.setBusinessStatus(SignedBusinessStatusEnum.getByNo(business.getBusinessSecStatus()).getDes());
				 }
				 Long queryId = null;
				Signed signed =  signedService.selectById(business.getId());
				if(signed.getOldSignedId()!=null){
					queryId = signed.getOldSignedId(); 
				}else{
					queryId = business.getId();
				}
				HaveReceiveRefundDto hrrDto = new HaveReceiveRefundDto();
				hrrDto.setRelationId(queryId);
				hrrDto.setDocType(DocTypeEnum.COLLECTION.getNo());
				hrrDto.setReceivablesType(ReceivablesTypeEnum.SIGNED.getNo());
				hrrDto.setTransferStatus("0");
				Double havereceive = haveReceiveRefundService.haveReceiveAmount(hrrDto);
				business.setReceived(havereceive==null?BigDecimal.ZERO:new BigDecimal(havereceive));
				receivablesType = ReceivablesTypeEnum.SIGNED.getNo();
			}
			//已退金额
			HaveReceiveRefundDto hrrDto = new HaveReceiveRefundDto();
			hrrDto.setRelationId(business.getId());
			hrrDto.setDocType(DocTypeEnum.REFUND.getNo());
			hrrDto.setReceivablesType(receivablesType);
			hrrDto.setTransferStatus("0");
			Double retired = haveReceiveRefundService.haveReceiveAmount(hrrDto);
			business.setRetired(retired==null?BigDecimal.ZERO:new BigDecimal(retired));
		}
		return list;
	}

}

